package optional;


import lombok.Builder;
import lombok.Data;

import java.util.Optional;

/**
 * @author Summerday
 */
public class OptionalTest {

    public static void main(String[] args) {
        OptionalTest test = new OptionalTest();
        String name1 = test.getName1(null);
        String name2 = test.getName2(User.builder().name("summerday").build());

    }

    public String getName1(User user) {
        if (user == null) {
            throw new RuntimeException("user不能为null!");
        }
        return user.getName();
    }

    public String getName2(User user) {
        return Optional.ofNullable(user)
                .map(User::getName)
                .orElseThrow(() -> new RuntimeException("user不能为null"));
    }


}

@Builder
@Data
class User {
    String name;
}
